package com.zhuoyue.biz.core.command.impl;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhuoyue.biz.core.command.Device;
import com.zhuoyue.biz.core.command.constant.LianKongBangDingPeiZhiControlCmdDeal;
import com.zhuoyue.common.utils.CmdUtils;
import org.apache.commons.lang3.StringUtils;


/**
 * 多联多控配置控制类
 *
 * @author Administrator
 */
public class LianKongBangDingPeiZhi implements Device {

    private static final String OD_0FAA = "0FAA";
    private static final String SOURCE_OD_INDEX = "sourceODIndex";

    @Override
    public String getCommand(String controlParam) {

        Map<String, String> map = (Map) JSONObject.parse(controlParam);

        String controlType = map.get("controlType");
        if (StringUtils.isEmpty(controlType)) {
            return null;
        }

        String cmd = "";
        if (LianKongBangDingPeiZhiControlCmdDeal.ControlType.ADD.equals(controlType)) {
            cmd = getAddCmd(map);
        } else if (LianKongBangDingPeiZhiControlCmdDeal.ControlType.DELETE.equals(controlType)) {
            cmd = getDelateCmd(map);
        } else if (LianKongBangDingPeiZhiControlCmdDeal.ControlType.CLEAR.equals(controlType)) {
            cmd = getClearCmd(map);
        }

        return cmd;

    }


    /**
     * 获取联控绑定新增命令
     *
     * @param map
     * @return
     */
    private String getAddCmd(Map<String, String> map) {


        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));
        if (OD_0FAA.equals(actionsMap.get(SOURCE_OD_INDEX))) {
            String cmd8 = "0" + actionsMap.get("index");

            String cmd10 = "0" + actionsMap.get("sourceWayNumber");

            String cmd11 = "0" + actionsMap.get("goalWayNumber");


            String cmd3To14 = LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_3 +
                    LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_4 +
                    map.get("gatewayMac") +
                    map.get("sourceDeviceMac") +
                    LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_7 +
                    cmd8 +
                    LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_9 +
                    cmd10 +
                    cmd11 +
                    actionsMap.get("goalDeviceMac") +
                    LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_14;

            String cmd2 = CmdUtils.getDateLength(cmd3To14);

            String cmd15 = CmdUtils.getCmdBodySum(cmd3To14);

            String fullCmd = LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_1 +
                    cmd2 +
                    cmd3To14 +
                    cmd15 +
                    LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_16;

            return fullCmd;
        } else {
            return getNewAddCmd(map);
        }

    }


    /**
     * 获取联控绑定删除命令
     *
     * @param map
     * @return
     */
    private String getDelateCmd(Map<String, String> map) {

        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));
        if (OD_0FAA.equals(actionsMap.get(SOURCE_OD_INDEX))) {

            String cmd3To14 = LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_3 +
                    LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_4 +
                    map.get("gatewayMac") +
                    map.get("sourceDeviceMac") +
                    LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_7 +
                    map.get("indexId") +
                    LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_9 +
                    LianKongBangDingPeiZhiControlCmdDeal.Clear.CMD_10 +
                    LianKongBangDingPeiZhiControlCmdDeal.Clear.CMD_17 +
                    LianKongBangDingPeiZhiControlCmdDeal.Clear.CMD_10;

            String cmd2 = CmdUtils.getDateLength(cmd3To14);

            String cmd15 = CmdUtils.getCmdBodySum(cmd3To14);

            String fullCmd = LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_1 +
                    cmd2 +
                    cmd3To14 +
                    cmd15 +
                    LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_16;
            return fullCmd;
        } else {
            return getNewDelateCmd(map);
        }
    }


    private String getClearCmd(Map<String, String> map) {

        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));
        if (OD_0FAA.equals(actionsMap.get(SOURCE_OD_INDEX))) {


            String cmd10 = "FFFF";

            String cmd11 = "0000";

            String cmd12 = "00000000";

            String cmd13 = "0000";

            String cmd14 = "FFFF";


            String cmd3To14 = LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_3 +
                    LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_4 +
                    map.get("gatewayMac") +
                    map.get("sourceDeviceMac") +
                    LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_7 +
                    map.get("indexId") +
                    LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_9 +
                    cmd10 +
                    cmd11 +
                    cmd12 +
                    cmd13 +
                    cmd14;

            String cmd2 = CmdUtils.getDateLength(cmd3To14);

            String cmd15 = CmdUtils.getCmdBodySum(cmd3To14);

            String fullCmd = LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_1 +
                    cmd2 +
                    cmd3To14 +
                    cmd15 +
                    LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_16;

            return fullCmd;
        } else {
            return getNewClearCmd(map);
        }
    }

    private String dealGoalWayNumber(String goalWayNumber) {

        int way1 = 0;
        int way2 = 0;
        int way3 = 0;
        int way4 = 0;
        int way5 = 0;
        int way6 = 0;
        int way7 = 0;
        int way8 = 0;

        String[] goalWayNumberArr = goalWayNumber.split(",");

        for (int i = 0; i < goalWayNumberArr.length; i++) {
            int way = Integer.parseInt(goalWayNumberArr[i]);
            if (way == 1) {
                way1 = LianKongBangDingPeiZhiControlCmdDeal.GoalNodeWayNumber.WAY_ARR[way - 1];
            } else if (way == 2) {
                way2 = LianKongBangDingPeiZhiControlCmdDeal.GoalNodeWayNumber.WAY_ARR[way - 1];
            } else if (way == 3) {
                way3 = LianKongBangDingPeiZhiControlCmdDeal.GoalNodeWayNumber.WAY_ARR[way - 1];
            } else if (way == 4) {
                way4 = LianKongBangDingPeiZhiControlCmdDeal.GoalNodeWayNumber.WAY_ARR[way - 1];
            } else if (way == 5) {
                way5 = LianKongBangDingPeiZhiControlCmdDeal.GoalNodeWayNumber.WAY_ARR[way - 1];
            } else if (way == 6) {
                way6 = LianKongBangDingPeiZhiControlCmdDeal.GoalNodeWayNumber.WAY_ARR[way - 1];
            } else if (way == 7) {
                way7 = LianKongBangDingPeiZhiControlCmdDeal.GoalNodeWayNumber.WAY_ARR[way - 1];
            } else if (way == 8) {
                way8 = LianKongBangDingPeiZhiControlCmdDeal.GoalNodeWayNumber.WAY_ARR[way - 1];
            }
        }

        int goalWayNumberHex = way1 | way2 | way3 | way4 | way5 | way6 | way7 | way8;

        return CmdUtils.intToHexTwo(goalWayNumberHex);

    }


    public static void main(String[] args) {
        System.out.println(1 | 4 | 10 | 64);
    }


    /**
     * 八路联控绑定新增新增接口
     *
     * @param map
     * @return
     */
    private String getNewAddCmd(Map<String, String> map) {

        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));

        String cmd8 = "0" + actionsMap.get("index");

        String cmd10 = actionsMap.get("sourceWayNumber");

        String cmd11 = actionsMap.get("goalWayNumber");


        String cmd3To14 = LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_3 +
                LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_4 +
                map.get("gatewayMac") +
                map.get("sourceDeviceMac") +
                LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_7 +
                //21 index
                LianKongBangDingPeiZhiControlCmdDeal.Delete.CMD_8 +
                //01
                LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_4 +
                //"0100"+"0400"+
                //sourceWayNumber源节点第X路
                transition(cmd10) +
                //goalWayNumber目标节点第X路
                transition(cmd11) +
                //目标节点MAC
                actionsMap.get("goalDeviceMac") +
                //设备功能ID号
                actionsMap.get("sourceODIndex") +
                LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_14;

        String cmd2 = CmdUtils.getDateLength(cmd3To14);

        String cmd15 = CmdUtils.getCmdBodySum(cmd3To14);

        String fullCmd = LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_1 +
                cmd2 +
                cmd3To14 +
                cmd15 +
                LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_16;

        return fullCmd;
    }

    /**
     * 八路联控绑定删除新增接口
     *
     * @param map
     * @return
     */
    private String getNewDelateCmd(Map<String, String> map) {

        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));

        String cmd11 = actionsMap.get("goalWayNumber");

        String cmd10 = actionsMap.get("sourceWayNumber");


        String cmd3To14 = LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_3 +
                LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_4 +
                map.get("gatewayMac") +
                map.get("sourceDeviceMac") +
                LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_7 +
                //子索引21
                LianKongBangDingPeiZhiControlCmdDeal.Delete.CMD_8 +
                //绑定子命令ID，删除
                LianKongBangDingPeiZhiControlCmdDeal.Delete.CMD_9 +
                transition(cmd10) +
                transition(cmd11) +
                actionsMap.get("goalDeviceMac") +
                actionsMap.get("sourceODIndex") +
                LianKongBangDingPeiZhiControlCmdDeal.Delete.CMD_14;
//校验和
        String cmd2 = CmdUtils.getDateLength(cmd3To14);
//帧尾
        String cmd15 = CmdUtils.getCmdBodySum(cmd3To14);

        String fullCmd = LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_1 +
                cmd2 +
                cmd3To14 +
                cmd15 +
                LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_16;

        return fullCmd;
    }

    /**
     * 八路清空新增接口
     *
     * @param map
     * @return
     */
    private String getNewClearCmd(Map<String, String> map) {

        String cmd10 = "FFFF";

        String cmd11 = "FFFF";

        String cmd12 = "FFFFFFFFFFFFFFFF";

        String cmd13 = "FFFF";

        String cmd14 = "FFFF";


        String cmd3To14 = LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_3 +
                LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_4 +
                map.get("gatewayMac") +
                map.get("sourceDeviceMac") +
                LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_7 +
                LianKongBangDingPeiZhiControlCmdDeal.Delete.CMD_8 +
                LianKongBangDingPeiZhiControlCmdDeal.Clear.CMD_9 +

                cmd10 +
                cmd11 +
                cmd12 +
                cmd13 +
                cmd14;

        String cmd2 = CmdUtils.getDateLength(cmd3To14);

        String cmd15 = CmdUtils.getCmdBodySum(cmd3To14);

        String fullCmd = LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_1 +
                cmd2 +
                cmd3To14 +
                cmd15 +
                LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_16;

        return fullCmd;
    }

    /**
     * 数据帧转化
     *
     * @param cmdN
     * @return
     */
    private String transition(String cmdN) {
        int ten = 10;
        int n = Integer.parseInt(cmdN);
        //10进制转化为16进制
        String a = null;
        int cmd16Jz = (int) Math.pow(2, n - 1);
        //把int转为String
        String hex = Integer.toHexString(cmd16Jz);
        int var = Integer.parseInt(hex);
        System.out.println(var);

        if (var > 0 && var < ten) {
            a = "0" + var + "00";
        }
        if (var >= ten) {
            a = var + "00";
        }
        String a16Jz = a.toString();
        return a16Jz;
    }


}
